<?php
/**
 * @version		$Id:  $
 * @package		Success Framework Mobile
 * @subpackage	File
 * @copyright	Copyright (C) 2007 - 2010 Success company - VietNam. All rights reserved.
 */
defined('PATH_BASE') or die();

class sscls_File 
{
    /**
	 * Load file
	 */
     
    function load($path='',$tmpValue=array())
    {           
        if($this->fileexists($path))
        {   
            if(!empty($tmpValue)){ 
                foreach($tmpValue as $key=>$value)
                {
                    $this->$key=$value;
                } 
            }
            include($path);    
        }
        else
            die('File '.$path.' not found!');
    }
    /**
	 * Import lib
	 */
     
    function import($path='')
    {
        if($this->fileexists($path))
        {
            require_once($path);    
        }
        else
            die('File '.$path.' not found!');
    }
    
    /**
	 * Check file is exists
     * @return true if file is exitsts else is not exists
	 */
    function fileexists($path)
    {
        if(file_exists($path))
            return true;
        else
            return false;
        
    }
    /**
	 * Upload file
     * @return Object
	 */
     
    function uploadfile($file=null,$filename='images',$path='uploads/',$return='index.php',$MAX_FILE_SIZE=10000000,$typefile='txt,doc,pdf,gif,jpeg,png,jpg,docx,xls,csv',$resize=false,$widththum=220,$heightthum=175,$paththum='uploads/thums/')
    {
        global $main;
        $object=$main->get('object');
        $object->add('msg',$msg);
        $object->add('suc',true);
        $object->add('filename','');
            if ( !isset($file["error"]) || $file["error"]!= 0 ) {
    
                   if($file["error"]==1)
                   {
                        $msg=Resources::_('Maximum size is 10M in sever');
                        $object->msg=$msg;
                        $object->suc=false;
                        return $object;
                   }
    
                   if($file["error"]==3)
                   {
                        $msg=Resources::_('Transmission line fault');
                        $object->msg=$msg;
                        $object->suc=false;
                        return $object;
                   }       
                    if($file["error"]==4)
                   {
                        $msg=Resources::_('Not file');
                        $object->msg=$msg;
                        $object->suc=false;
                        return $object;
                   }                             
                }
                 if ( $file["size"] > $MAX_FILE_SIZE ) {
                         $msg=Resources::_('Maximum size is ').$MAX_FILE_SIZE;
                       $object->msg=$msg;
                        $object->suc=false;
                        return $object;
                    }
                $temp = preg_split('/[\/\\\\]+/', $file["name"]);
                $filename1 = $temp[count($temp)-1];
                $namefile=md5($filename).'.';
                $typefile=substr($filename1,-3);
                $filename=$namefile.$typefile;
                if($typefile!='gif'&& $typefile!='png'&& $typefile!='jpg'&& $typefile!='jpeg')
                {
                    $msg=Resources::_('This file type is not supported.');
                    $object->msg=$msg;
                    $object->suc=false;
                    return $object;
                }
                    
                 $upload_dir =$path;
                $upload_file = $upload_dir . $filename;
                if ( move_uploaded_file($file["tmp_name"], $upload_file) ) {
                    if($resize==true)
                    {
                        File::import('resources/resizeimage.inc.php');
                        $rimg=new RESIZEIMAGE($upload_file);
                        $rimg->resize_limitwh($widththum,$heightthum,$paththum.$filename);
                        $rimg->close();    
                    }
                        $msg=Resources::_('Upload success!.');       
                        $object->msg=$msg;
                        $object->suc=true;
                        $object->filename=$filename;
                        return $object;
                    
                } else {
                    
                    $msg=Resources::_('Error Upload');
                    $object->msg=$msg;
                    $object->suc=false;
                    return $object;
                
                } //end if   

        
    }
     /**
	 * Download file hidden link
     * @return void
	 */
    function downloadfile($file_url='',$namefile='file')
        {
            @header('Content-type: application/octetstream');
            @header("Pragma: public");
            @header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
            @header('Content-Disposition: attachment; filename='.$namefile);
            ob_end_clean ();
            ob_start();
            readfile ($file_url);	
            $size=ob_get_length(); 
            header("Content-Length: $size"); 
            ob_end_flush();	
            exit();
        }
    /*    
    function createthum($filename='',$path1='',$path2='',$widththum='100',$heightthum='100')
    {
                File::import('resources/resizeimage.inc.php');
                $rimg=new RESIZEIMAGE($upload_file);
                $rimg->resize_limitwh($widththum,$heightthum,$paththum.$filename);
                $rimg->close();  
    }
    */
    
     /**
	 * Delete file
     * @param path is path to file to delete
     * @return return true if deleted else return false
	 */
    
    function deletefile($path='')
    {
            //Ser permistion to read,write and ative
			@chmod($path, 0777);

			// In case of restricted permissions we zap it one way or the other
			// as long as the owner is either the webserver or the ftp
			if (@unlink($path)) {
			     return true;
			 }
             else
             return false;
    }

}
?>